export default {
  formatDate(val, type) {
    if(!val||val=='') return ''
    if (typeof (val) == 'string') {
      val = val.replace(/-/g, '/')
    }
    let start = new Date(val)
    let y = start.getFullYear()+''
    let m = (start.getMonth() + 1) >= 10 ? (start.getMonth() + 1) : '0' + (start.getMonth() + 1)
    let d = start.getDate() >= 10 ? start.getDate() : '0' + start.getDate()
    let h = start.getHours()
    let mm = start.getMinutes() >= 10 ? start.getMinutes() : '0' + start.getMinutes()
    let s = start.getSeconds() >= 10 ? start.getSeconds() : '0' + start.getSeconds()
    let str = y + '-' + m + '-' + d
    if (type == 'yyyy') {
      str = y
    } else if (type == 'MM') {
      str = m
    } else if (type == 'yyyy-MM') {
      str = y + '-' + m
    } else if (type == 'MM-dd') {
      str = m + '-' + d
    } else if (type == 'yyyy-MM-dd HH:mm:ss') {
      str = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
    }else if (type == 'yyyy年MM月dd日 HH:mm:ss') {
      str = y + '年' + m + '月' + d + '日 ' + h + ':' + mm + ':' + s
    } else if (type == 'HH:mm:ss') {
      str = h + ':' + mm + ':' + s
    }else if (type == 'HH:mm') {
      str = h + ':' + mm
    }
    return str
  },
  //判断是否是空对象或空数组
  isEmpty(value) {
    return (Array.isArray(value) && value.length === 0) ||
      (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
  },
  getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  },
  //封装过期控制代码
  setVal(key, value) {
    let curTime = new Date().getTime();
    sessionStorage.setItem(key, JSON.stringify({
      data: value,
      time: curTime
    }));
  },
  getVal(key, exp) {
    if (!exp) {
      exp = 1000 * 60 * 60 * 24*10
    }
    let data = sessionStorage.getItem(key)
    let dataObj = JSON.parse(data);
    let dataObjDatatoJson = null;
    if (dataObj && (new Date().getTime() - dataObj.time < exp)) {
      dataObjDatatoJson = dataObj.data;
    }
    return dataObjDatatoJson;
  },
}
